/*定义一些工具函数*/
(function (window) {
    var CK = {
        request: function (data) {//重构Ajax函数，使得可以调用config里配置的主机地址
            $.ajax({
                url: config.homePath + data.url,
                data: data,
                type: data.type || "post",
                async: async || "true",
                success: success(),
            })
        },

        load: function (url, $dom) {
            if (!$dom) {
                $("#container").load(url + ".html");
            } else {
                $dom.load(url + ".html")
            }
            //延时加载，防止因为网络问题导致页面未加载完全时加载js
            setTimeout(function () {
                $.getScript(url + ".js");
            }, 300);
        },

        append: function (url, $dom) {
            if (!$dom) {
                $("#container").append($.get(url + ".html"));
            } else {
                $dom.append($.get(url + ".html"))
            }
            //延时加载，防止因为网络问题导致页面未加载完全时加载js
            setTimeout(function () {
                $.getScript(url + ".js");
            }, 300);
        },


        alert: function (str) {
            var modal = $('#alertBlock'), close = modal.find('[name=alertClose]'), overlay = $("[name=md-overlay]");
            $("#alertStr").html(str);
            modal.addClass('md-show');
            overlay.unbind("click", removeModalHandler);
            overlay.bind("click", removeModalHandler);
            setTimeout(function () {
                $(document.documentElement).addClass('md-perspective');
            }, 25);

            function removeModalHandler() {
                $('#alertBlock').removeClass('md-show');
                $(".md-setperspective").removeClass("md-setperspective");
                $(document.documentElement).removeClass("md-perspective");
            }


            close.click(function (ev) {
                ev.stopPropagation();
                removeModalHandler();
            });

        },
        formatUrl: function (url) {
            return config.homePath + url;
        },

        initMobileIput: function ($dom) {
            $dom.bind('input propertychange', function () {
                var ok = /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/.test($dom.val());

                if (ok == false) {
                    $dom.parent("div").addClass("has-error");
                    $("#alertDiv").html(temple({strongStr: "手机号码格式不正确"}));
                } else {
                    $dom.parent("div").removeClass("has-error");
                    $("#alertDiv").html("");
                }
                //debugger;
            });

        },

        navigator: function () {
            //如果有记录从哪里来就使用记录来源位置
            if (config.hookPosition) {
                window.location.href = config.hookPosition;
            } else {
                window.location.href = "#homeShow";
            }
        },
        copyToClipboard: function (maintext) {
            if (window.clipboardData) {
                window.clipboardData.setData("Text", maintext);
            } else if (window.netscape) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
                } catch (e) {
                    alert("该浏览器不支持一键复制！n请手工复制文本框链接地址～");
                }
                var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
                if (!clip) return;
                var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
                if (!trans) return;
                trans.addDataFlavor('text/unicode');
                var str = new Object();
                var len = new Object();
                var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                var copytext = maintext;
                str.data = copytext;
                trans.setTransferData("text/unicode", str, copytext.length * 2);
                var clipid = Components.interfaces.nsIClipboard;
                if (!clip) return false;
                clip.setData(trans, null, clipid.kGlobalClipboard);
            }
            alert("以下内容已经复制到剪贴板: " + maintext);
        },
        toUtf8: function (str) {
            var out, i, len, c;
            out = "";
            len = str.length;
            for (i = 0; i < len; i++) {
                c = str.charCodeAt(i);
                if ((c >= 0x0001) && (c <= 0x007F)) {
                    out += str.charAt(i);
                } else if (c > 0x07FF) {
                    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                } else {
                    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                }
            }
            return out;
        }

    };
    window.CK = CK;
})(window);

